<?php
namespace Tbf\Component\Crypto\Password\Plan;
use Tbf\Component\Crypto\Password\PlanInterface;
use Tbf\Component\Strings\StringFunction;
/**
* ecshop 用户密码方案
* 加密后的密码格式
* 原文a123456 加密后 $ecshop$3959$04affd4751ad4b43cb2c4c06342de454
* 不要用这个方案加密密码...(不安全)
*/
class EcshopPlan implements PlanInterface
{
    public function hash($plain)
    {
        $salt = mt_rand(0,9999);
        $encrypted = md5(md5($plain).$salt);

        return '$ecshop$'.$salt.'$'.$encrypted;
    }
    public function verify($plain,$hashed)
    {
        $data = explode('$',$hashed);
        $salt = $data[2];
        $hashed_encrypted = $data[3];
        $plain_encrypted = md5(md5($plain).$salt);

        return $hashed_encrypted==$plain_encrypted;
    }
    public function support($hashed)
    {
        if (!StringFunction::startWith($hashed,'$ecshop$')) {
            return false;
        }

        return true;
    }
}
